package cn.iocoder.yudao.module.product.selection.dao;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.mapper.CoreMapper;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreLambdaQueryWrapper;
import cn.iocoder.yudao.module.product.selection.pojo.po.ProductSupplySelection;
import org.apache.ibatis.annotations.Mapper;

import java.util.Collection;
import java.util.List;

@Mapper
public interface ProductSupplySelectionMapper extends CoreMapper<ProductSupplySelection> {

    default ProductSupplySelection selectBySpuId(Long spuId) {
        return selectOne(ProductSupplySelection::getSpuId, spuId);
    }

    default List<ProductSupplySelection> selectBySpuIds(Collection<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return null;
        }
        CoreLambdaQueryWrapper<ProductSupplySelection> wrapper = new CoreLambdaQueryWrapper<ProductSupplySelection>()
                .in(ProductSupplySelection::getSpuId, spuIds);
        return selectList(wrapper);
    }

    default Long selectSpuIdBySkuId(Long skuId) {
        List<Long> objs = selectObjs(new CoreLambdaQueryWrapper<ProductSupplySelection>()
                .select(ProductSupplySelection::getSpuId)
                .apply("FIND_IN_SET({0}, sku_ids) > 0", skuId));
        return CollUtil.isEmpty(objs) ? null : objs.get(0);
    }

}
